package com.filldream.sun.core;

import com.filldream.sun.bean.result.ErrorCode;

import java.lang.reflect.Method;

/**
 * 反射工具类
 * @author RickSun
 * @see 2019-12-13
 */
public class FieldUtil {
    public static String getFieldValueByFieldName(String fieldName, Object object) {
        Assert.isNull(object, ErrorCode.PARAM_ERR.setContent("反射对象为空"));
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = object.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(object, new Object[] {});
            return JsonUtil.toJson(value);
        } catch (Exception e) {
            return null;
        }
    }

}